-- UITrainingItemDesc
-- Create by cheny3
-- 英雄进阶信息界面

-- UITrainingItemDesc继承自Layer
UITrainingItemDesc = class("UITrainingItemDesc", function()
    return cc.Layer:create();
end);

function UITrainingItemDesc.create(heroId, rank)
    return UITrainingItemDesc.new(heroId, rank);
end

local PROP_NUM = 5;

-- 构造函数
function UITrainingItemDesc:ctor(heroId, rank)
    self:setName("UITrainingItemDesc");
    local node = cc.CSLoader:createNode("layout/training/TrainingItemDesc.csb");
    self:addChild(node);
    self.node = node;
    self.heroId = heroId;
    self.rank = rank;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册点击事件
function UITrainingItemDesc:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UITrainingItemDesc");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end

-- 重绘
function UITrainingItemDesc:redraw()
    local title = findChildByName(self.node, "CT/title");
    local heroDesc = findChildByName(self.node, "CT/hero_desc");
    local propNode = findChildByName(self.node, "CT/prop_node");

    -- 冈布奥名字
    local name = PetM.query(self.heroId, "name");
    local titleDesc = name;
    TextStyleM.setTextStyle(title, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    title:setString(titleDesc);

    -- 进阶星级显示
    local starNode = findChildByName(self.node, "CT/star_node");
    local pet = PetM.getMyPet(self.heroId);
    local rank = self.rank or pet:getRank();

    -- 生成星星层，直接挂到结点上
    local starPath = "images/ui/symbol/star_large.png";
    -- 如果冈布奥已觉醒，使用橙色的星星
    if AwakeM.isCarved(self.heroId) then
        starPath = "images/ui/symbol/star_large3.png";
    end

    local layer = createStarLayer(rank, starPath, "right", -0.7);
    if not layer then
        return;
    end
    starNode:removeAllChildren();
    starNode:addChild(layer);

    -- 英雄介绍
    local descText = PetM.query(self.heroId, "desc");
    TextStyleM.setTextStyle(heroDesc, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    heroBriefLabel = applyString(heroDesc, descText);

    -- 技能提升
    local arr = {
        {"talent_skill_level", rank},}

    local labelList = {};
    for index, value in pairs(arr) do
        local desc;
        if value[2] > 0 then
            local name = FieldsM.getFieldName(value[1]);
            if value[1] == "max_hp" then
                name = FieldsM.getFieldName("hp");
            elseif value[1] == "max_mp" then
                name = FieldsM.getFieldName("mp");
            elseif value[1] == "talent_skill_level" then
                name = getLocStr("talent_skill_level");
            elseif value[1] == "career_type" then
                name = string.format(getLocStr("open_career_type"), getNameByDep(value[2]));
            end

            if value[1] == "career_type" then
                desc = name;
            elseif value[1] == "talent_skill_level" then
                desc = name .. " LV." .. value[2];
            else
                desc = name .. "+" .. value[2];
            end
        else
            desc = "";
        end

        labelList[index] = desc;
    end

    for i = 1, #labelList do
        local propLabel = propNode:getChildByName("prop"..i);
        local desc = labelList[i];
        TextStyleM.setTextStyle(propLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE);
        applyString(propLabel, desc);
    end
end

-- 适配
function UITrainingItemDesc:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end